package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.ChangUserInfoBeanTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangUserInfoActivityTing extends AppCompatActivity {

    private EditText etChangnichengTing;
    private RadioGroup sexTing;
    private RadioButton sexNanTing;
    private RadioButton sexNvTing;
    private RadioButton sexCheckTing;
    private EditText etChangxuehaoTing;
    private EditText etChangbanjiTing;
    private EditText etChangxingmingTing;
    private EditText etChangqianmingTing;
    private Button registerBtnTing;
    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView regBtnTing;
    private String nicheng_ting,sex_ting,qianming_ting,xuehao_ting,banji_ting,xingming_ting,sexsex;
    private int id_ting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chang_user_info_ting);
        initView();
        getData();
    }

    //获取从个人资料传递过来的参数
    private void getData() {
        id_ting = getIntent().getIntExtra("id",0);
        setInput(etChangnichengTing,"NickName");
        setInput(etChangxingmingTing,"StuName");
        setInput(etChangxuehaoTing,"StuNo");
        setInput(etChangbanjiTing,"StuClass");
        setInput(etChangqianmingTing,"Signature");
        sex_ting = getIntent().getStringExtra("Sex");
//        Log.i("gr",sex_ting);
        rediogroup(sex_ting);
    }

    //封装一个解析参数的方法
    private void setInput(EditText etChang, String extra) {
        String val = getIntent().getStringExtra(extra);
        etChang.setText(val);
//        etChang.setSelection(val.length());
    }

    //单选框选中
    private void rediogroup(String redio){
        Log.i("ww",redio);
        if(redio.equals("0")){
            sexTing.check(R.id.sex_nan_ting);
        }else if(redio.equals("1")){
            sexTing.check(R.id.sex_nv_ting);
        }else if(redio.equals("2")){
            sexTing.check(R.id.sex_baomi_ting);
        }
    }

    //初始化控件
    private void initView() {
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        regBtnTing = (TextView) findViewById(R.id.reg_btn_ting);
        etChangnichengTing = (EditText) findViewById(R.id.et_changnicheng_ting);
        sexTing = (RadioGroup) findViewById(R.id.sex_ting);
        sexNanTing = (RadioButton) findViewById(R.id.sex_nan_ting);
        sexNvTing = (RadioButton) findViewById(R.id.sex_nv_ting);
        etChangxuehaoTing = (EditText) findViewById(R.id.et_changxuehao_ting);
        etChangbanjiTing = (EditText) findViewById(R.id.et_changbanji_ting);
        etChangxingmingTing = (EditText) findViewById(R.id.et_changxingming_ting);
        etChangqianmingTing = (EditText) findViewById(R.id.et_changqianming_ting);
        registerBtnTing = (Button) findViewById(R.id.grzl_btn_ting);
        regBtnTing.setText("修改资料");
        //获取选中的单选框
//        sexCheckTing = (RadioButton) findViewById(sexTing.getCheckedRadioButtonId());
//        sex_ting = sexCheckTing.getText().toString();
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangUserInfoActivityTing.this.finish();
            }
        });
        sexTing.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                String sex = radioButton.getText().toString();
                sexsex = (sex.equals("男"))?"0":(sex.equals("女"))?"1":"2";
//                Log.i("2222",sex);
            }
        });
        registerBtnTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_click();
            }
        });
    }

    //修改资料按钮点击事件
    private void btn_click() {
        getEditString();
        JsonObject jo = jo();
//        Log.i("hha",new Gson().toJson(jo));
        OkhttpUtilsTing.isPOST("AndroidUser/update", false, jo,new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
//                final loginResultBeanTing<ChangUserInfoBeanTing> r = gson.fromJson(res, new TypeToken<loginResultBeanTing<ChangUserInfoBeanTing>>(){}.getType());
                final DataBeanTing<ChangUserInfoBeanTing> r = gson.fromJson(res, new TypeToken<DataBeanTing<ChangUserInfoBeanTing>>(){}.getType());
                ChangUserInfoActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        Log.i("hh",gson.toJson(r));
                        if(r.code == 200){
                            setData(r.data);
                            Log.i("hh",gson.toJson(r));
                        }
                    }
                });
            }
        });
    }

    //页面销毁时回传的参数
    private void setData(ChangUserInfoBeanTing result) {
        Intent data = new Intent();
        data.putExtra("NickName",result.nickName);
        data.putExtra("Signature",result.signature);
        data.putExtra("StuNo",result.stuNo);
        data.putExtra("StuClass",result.stuClass);
        data.putExtra("StuName",result.stuName);
        data.putExtra("Sex",result.sex);
        setResult(RESULT_OK,data);
        new AestheticDialog.Builder(ChangUserInfoActivityTing.this, DialogStyle.EMOTION, DialogType.SUCCESS)
                .setTitle("SUCCESS")
                .setMessage("修改资料成功")
                .setDuration(2000)
                .show();
        Timer timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                ChangUserInfoActivityTing.this.finish();//关闭修改资料页面
            }
        };
        timer.schedule(task,2000);
    }

    private JsonObject jo(){
        JsonObject jo = new JsonObject();
        jo.addProperty("id",new Gson().toJson(id_ting));
        jo.addProperty("nickName",nicheng_ting);
        jo.addProperty("signature",qianming_ting);
        jo.addProperty("stuNo",xuehao_ting);
        jo.addProperty("stuClass",banji_ting);
        jo.addProperty("stuName",xingming_ting);
        jo.addProperty("sex",sexsex);
        return jo;
    }

    private void getEditString(){
        banji_ting=etChangbanjiTing.getText().toString().trim();
        xuehao_ting=etChangxuehaoTing.getText().toString().trim();
        xingming_ting=etChangxingmingTing.getText().toString().trim();
        qianming_ting=etChangqianmingTing.getText().toString().trim();
        nicheng_ting=etChangnichengTing.getText().toString().trim();
    }

}